/**
 * @Title: VideoFragment.java
 * @Package com.sinoglobal.waitingMe.activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @author hpq
 * @date 2015-8-30 下午1:53:32
 */
	
package com.sinoglobal.wallet.fragment;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.activity.W_BalanceDetailActivity;
import com.sinoglobal.wallet.activity.W_MyBankActivity;
import com.sinoglobal.wallet.activity.W_PayPasswordActivity;
import com.sinoglobal.wallet.activity.W_PayPasswordUpdateActivity;
import com.sinoglobal.wallet.activity.W_WithDrawalActivity;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.app.SinoConstans;
import com.sinoglobal.wallet.entity.W_HomeBalanceEntity;
import com.sinoglobal.wallet.entity.W_HomeBalanceEntity.RsEntity;
import com.sinoglobal.wallet.receiver.W_ChangeFragmentBalanceReciver;
import com.sinoglobal.wallet.receiver.W_NetworkStatusReceiver;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.LogUtils;


/**
 * @author clf
 * @createdate 2015-8-30 下午1:53:32
 * @Description: W_HomeActivity代码一样，你把W_HomeActivity代码复制进来就可以了。现在没人用这个我就不维护了。浪费时间。想用的话复制代码过来就可以用；
 */
public class W_HomeFragment extends W_SinoBaseFragment implements OnClickListener {
	private TextView notice_text,account_balance_text,my_bankcard_num,account_balance_right_red_dot;
	private RelativeLayout account_balance,recharge_layout,withdrawal_layout,my_bankcard_layout,pay_password_layout,coupons_layout;
	private String blanceTxt="0.0";
	private String isHaspass="";
//	private int bankCardNum;
	private W_ChangeFragmentBalanceReciver ChangeBalanceReciver;
	private W_NetworkStatusReceiver netStatusReceiver;
	private View view;
	public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view=inflater.inflate(R.layout.wallet_activity_main, null);
		initReciver();
		initView();
		monitorNetworkStatus();
		getData();
		return view;
	}
public void setBalance(String str){
	blanceTxt=str;
	account_balance_text.setText("￥"+str);
	account_balance_right_red_dot.setVisibility(View.VISIBLE);	
}
public void setisHasdPass(Boolean isHasPass){
	if(isHasPass){
	isHaspass="Y";
	notice_text.setVisibility(View.GONE);}
}
public void setBankCardNum(String str){
	if(str.equals("0")){
		my_bankcard_num.setText("未添加");
	}else{
	my_bankcard_num.setText(str+"张");
}}

	private void initView() {
		notice_text = (TextView)view.findViewById(R.id.notice_text);
		account_balance_text = (TextView)view.findViewById(R.id.account_balance_text);
		my_bankcard_num= (TextView)view.findViewById(R.id.my_bankcard_num);
		account_balance_right_red_dot= (TextView)view.findViewById(R.id.account_balance_right_red_dot);
		account_balance = (RelativeLayout)view.findViewById(R.id.account_balance);
		recharge_layout = (RelativeLayout)view.findViewById(R.id.recharge_layout);
		withdrawal_layout = (RelativeLayout)view.findViewById(R.id.withdrawal_layout);
		my_bankcard_layout = (RelativeLayout)view.findViewById(R.id.my_bankcard_layout);
		pay_password_layout = (RelativeLayout)view.findViewById(R.id.pay_password_layout);
		coupons_layout = (RelativeLayout)view.findViewById(R.id.coupons_layout);

		notice_text.setOnClickListener(this);
		account_balance.setOnClickListener(this);
		recharge_layout.setOnClickListener(this);
		withdrawal_layout.setOnClickListener(this);
		my_bankcard_layout.setOnClickListener(this);
		pay_password_layout.setOnClickListener(this);
		coupons_layout.setOnClickListener(this);
	}
	private void initReciver() {
		// TODO Auto-generated method stub
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(SinoConstans.KEY_BalanceReceiver);
		ChangeBalanceReciver = new W_ChangeFragmentBalanceReciver(getActivity());
		getActivity().registerReceiver(ChangeBalanceReciver, intentFilter);	
	}
	private void monitorNetworkStatus() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		netStatusReceiver = new W_NetworkStatusReceiver();
		getActivity().registerReceiver(netStatusReceiver, filter);
	}

	@Override
	public void findView(View contentView) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if(id==R.id.notice_text){
			Bundle setting_bundle = new Bundle();
			setting_bundle.putString("title_flag", "3");
			goIntent(W_PayPasswordActivity.class, setting_bundle);//设置密码
		}else if(id==R.id.account_balance){
			account_balance_right_red_dot.setVisibility(View.GONE);
			goIntent(W_BalanceDetailActivity.class);//账号余额
		}else if(id==R.id.recharge_layout){//充值
			Toast.makeText(getActivity(), "功能在路上，敬请期待", 0).show();
				//	goIntent(RechargelActivity.class);
		}else if(id==R.id.withdrawal_layout){//提现
			Bundle setting_bundle = new Bundle();
			setting_bundle.putString("IsHasPass", isHaspass);
			setting_bundle.putDouble("BlanceTxt", Double.valueOf(blanceTxt));
			goIntent(W_WithDrawalActivity.class,setting_bundle);
		}else if(id==R.id.my_bankcard_layout){//我的银行卡
			goIntent(W_MyBankActivity.class);
		}else if(id==R.id.pay_password_layout){//支付密码
			if(isHaspass.equals("N")){
				Bundle setting_bundle = new Bundle();
				setting_bundle.putString("title_flag", "3");
				goIntent(W_PayPasswordActivity.class, setting_bundle);//设置密码
			}else if(isHaspass.equals("Y")){
				goIntent(W_PayPasswordUpdateActivity.class);
			}
			
		}else if(id==R.id.coupons_layout){//优惠券
			Toast.makeText(getActivity(), "功能在路上，敬请期待", 0).show();
	}
	}
	
	private void getData() {
		MyAsyncTask<W_HomeBalanceEntity> task = new MyAsyncTask<W_HomeBalanceEntity>(true,getActivity(),true) {
			@Override
			public W_HomeBalanceEntity before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().getBalance(getActivity());
			}

			@Override
			public void after(W_HomeBalanceEntity result) {
				if (result != null) {
					if (result.getCode().equals("100")) {
						RsEntity rs=result.getRs();
						if(rs.getIsHasPass() != null){
							if(rs.getIsHasPass().equals("N")){
								notice_text.setVisibility(View.VISIBLE);
							}else{
								notice_text.setVisibility(View.GONE);
							}
						}
						blanceTxt = rs.getBalance();
						isHaspass = rs.getIsHasPass();
						account_balance_text.setText("￥"+rs.getBalance());
						if(rs.getBankCardNum()==0){
							my_bankcard_num.setText("未添加");	
						}else{
							my_bankcard_num.setText(rs.getBankCardNum()+"张");	
						}
						if("Y".equals(rs.getBalanceChange())){
							account_balance_right_red_dot.setVisibility(View.VISIBLE);	
						}else{
							account_balance_right_red_dot.setVisibility(View.GONE);	
						}
					}else{
						Toast.makeText(getActivity(), "数据加载失败", 0).show();
					}
				}

			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		getActivity().unregisterReceiver(ChangeBalanceReciver);
		getActivity().unregisterReceiver(netStatusReceiver);//网络监控注销
	}
}
